package com.lin.spzx.manager.config;


import com.lin.spzx.manager.interceptor.LoginAuthInterceptor;
import com.lin.spzx.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//解决跨域问题
@Component
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired//优化拦截路径加
    private UserAuthProperties userAuthProperties;

    //拦截器注册
    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor ;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                .excludePathPatterns("/admin/system/index/login" ,
                        "/admin/system/index/generateValidateCode",
                        "/admin/system/index/getUserInfo"
                        )
                //第一个优化配置，写在yml配置
                //.excludePathPatterns(userAuthProperties.getNoAuthUrls())
                .addPathPatterns("/**");
    }


        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")      // 添加路径规则
                    .allowCredentials(true)               // 是否允许在跨域的情况下传递Cookie
                    .allowedOriginPatterns("*")           // 允许请求来源的域规则
                    .allowedMethods("*")
                    .allowedHeaders("*") ;                // 允许所有的请求头
        }


}